package cn.shyouchi.wuguanjia.android.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.utils.CheckForAllUtils;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EmergenctContactActivity extends BaseActivity {

    @BindView(R.id.emergency_contact_name)
    EditText name;
    @BindView(R.id.emergency_contact_relationship)
    TextView relationship;
    @BindView(R.id.emergency_contact_telephone)
    EditText telephone;
    public  static final String EMERGENCY_CONTACT_NAME = "emergency_contact_name";
    public  static final String EMERGENCY_CONTACT_RELATIONSHIP = "emergency_contact_relationship";
    public static final String EMERGENCY_CONTACT_TELEPHONE = "emergency_contact_telephone";
    @BindView(R.id.topBar)
    TopBar topBar;

    @Override
    public int initContentView() {
        return R.layout.activity_emergenct_contact;
    }

    @Override
    public void initInjector() {

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        topBar.setTitle("填写紧急联系人");
        topBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(topBar.getToolbar());
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @OnClick(R.id.emergency_contact_save_button)
    void saveButtonTapped() {
        if (name.getText().toString().isEmpty()) {
            Toast.makeText(this, "请输入紧急联系人的姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (telephone.getText().toString().isEmpty()) {
            Toast.makeText(this, "请输入紧急联系人的电话", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!CheckForAllUtils.isMobileNO(telephone.getText().toString())) {
            Toast.makeText(this, "手机号码不正确", Toast.LENGTH_SHORT).show();
            return;
        }



        Intent resultIntent = new Intent();
        resultIntent.putExtra(EMERGENCY_CONTACT_NAME, name.getText().toString());
        resultIntent.putExtra(EMERGENCY_CONTACT_RELATIONSHIP, relationship.getText().toString());
        resultIntent.putExtra(EMERGENCY_CONTACT_TELEPHONE, telephone.getText().toString());
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }

    @OnClick(R.id.emergency_contact_relationship_container)
    void relationshipTapped() {
        // Hide the keyboard if necessary since the picker view is covered by the keyboard
        if (getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
        TextView tvTitle = (TextView) getLayoutInflater().inflate(R.layout.dialog_title, null);
        tvTitle.setText("选择关系");
        builder.setCustomTitle(tvTitle);
        final String[] graduates = new String[]{"父亲","母亲","兄弟姐妹","朋友","儿子","女儿"};
        builder.setItems(graduates, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                relationship.setText(graduates[which]);
            }
        });
        builder.setNegativeButton("取消", null);
        builder.create().show();
    }


}
